package com.meilele.biz.shiro;

import com.meilele.dao.shiro.UserMapper;
import com.meilele.entity.shiro.User;
import com.meilele.util.zhengchao.Contants;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class BaseBiz {
    @Resource
    private UserMapper dao;
    public void login(String username, String password) throws Exception{
        Subject user= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        user.login(token);
    }

    public User findByUsernameAndPassword(String username, String pwd){
        User user=dao.findByUsernameAndPassword(username);
        return user;
    }

    public void saveUser(User user) {
        String pw= new Md5Hash(user.getPassword(), Contants.SALT).toString();
            user.setPassword(pw);
        dao.insertSelective(user);
    }
}
